/*
 * configure-widget.c
 *
 * Copyright (C) 2011 - Lucas van Dijk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include "environment.h"

#define UI_FILE ANJUTA_DATA_DIR"/glade/anjuta_avr_environment.ui"
#define TARGETS_FILE ANJUTA_DATA_DIR"/avr_targets_list"

#define AVR_OPTIONS_FRAME "avr_options_frame"
#define TARGET_COMBO "target_combo"
#define CPU_FREQ_ENTRY "cpu_freq_entry"

static gboolean
on_configure_widget_destroy(GtkWidget* widget, gpointer userdata)
{
	AnjutaAVREnvironment *plugin = (AnjutaAVREnvironment*) userdata;

	g_free(plugin->target_device);
	g_free(plugin->cpu_freq);

	plugin->target_device = g_strdup (avr_configure_widget_get_target_device (
	                                                                          AVR_CONFIGURE_WIDGET(widget)));
	plugin->cpu_freq = g_strdup (avr_configure_widget_get_cpu_freq (
	                                                                AVR_CONFIGURE_WIDGET(widget)));
}

GtkWidget *
load_configure_widget(AnjutaAVREnvironment *plugin)
{
	AVRConfigureWidget *widget = avr_configure_widget_new();

	/* Connect destroy signal so we can free up some things when the dialog is
	 * finished.
	 */
	g_signal_connect(GTK_WIDGET(widget), "destroy", 
	                 G_CALLBACK(on_configure_widget_destroy), plugin);

	/* Load available devices from file */
	GFile *targets_file = g_file_new_for_path(TARGETS_FILE);
	GFileInputStream *input_stream = g_file_read(targets_file, NULL, NULL);
	int i = 0;
	int active = -1;
	
	if(input_stream)
	{
		/* Get filesize so we can allocate enough memory for the file contents */
		GFileInfo *info = g_file_query_info (targets_file, "standard::size", 0, NULL, NULL);

		if(info)
		{
			goffset size = g_file_info_get_size (info);

			/* Allocate buffer */
			gchar *buffer = g_new0 (gchar, size + 1);
			g_input_stream_read_all (G_INPUT_STREAM(input_stream), buffer, (gsize) size, NULL, NULL, NULL);

			/* Read all lines and put them in the combobox */
			gchar **lines = g_strsplit (buffer, "\n", 0);
			g_free (buffer);

			gchar **iter = lines;
			while(*iter)
			{
				if(g_strcmp0(*lines, plugin->target_device) == 0)
				{
					active = i;
				}

				avr_configure_widget_add_target_device (widget, *iter);
				iter++;
				i++;
			}

			g_strfreev (lines);
			g_object_unref(info);
		}

		g_object_unref(input_stream);
	}
	
	g_object_unref (targets_file);

	if(active >= 0)
	{
		avr_configure_widget_set_target_device (widget, active);
	}

	if(plugin->cpu_freq)
	{
		avr_configure_widget_set_cpu_freq (widget, plugin->cpu_freq);
	}		
	
	return GTK_WIDGET(widget);
}